package metrics_sql

const (
	K8sContainerState = `
app = "%v"
agent = "prometheus-%v-0"

query = (start=%v, stop=%v) => {
    conatiner_pod = from(bucket: "data")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
        |> filter(fn: (r) => r["_field"] == "kube_pod_container_status_ready")
        |> filter(fn: (r) => r["prometheus_replica"] == agent )
        |> filter(fn: (r) => r["namespace"] == app)
        |> last()
        |> filter(fn: (r) => r["pod"] != "jaeger-glue")
        |> filter(fn: (r) => r["container"] != "linkerd-proxy")
        |> map(fn: (r) => ({_field:"cell_status",_value: if r._value == 1.0 then "Ready" else "NotReady"  ,_time:r._time,container:r["container"],pod: r["pod"]}))
    pod_node = from(bucket: "data")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
        |> filter(fn: (r) => r["_field"] == "kube_pod_info")
        |> filter(fn: (r) => r["namespace"] == app)
        |> filter(fn: (r) => r["prometheus_replica"] == agent)
        |> filter(fn: (r) => r["job"] == "kube-state-metrics")
        |> last()
        |> filter(fn: (r) => r["pod"] != "jaeger-glue")
        |> map(fn: (r) => ({_field:"cell_status",_value: r._value ,_time:r._time,node:r["node"],pod: r["pod"]}))
    return join(tables: {conatiner_pod: conatiner_pod, pod_node: pod_node}, on: [ "pod"])
        |> map(fn: (r) => ({_field:"container_pod_node",_value:r._value_conatiner_pod ,_time:r._time_conatiner_pod, container:r.container,pod:r.pod,node:r.node}))
}

query()
`
	GluenetsContainerState = `
agent = "%v"
app = "%v"
cell = "%v"
name = app+"_" +cell

from(bucket: "data")
  |> range(start: %v, stop: %v)
  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
  |> filter(fn: (r) => r["_field"] == "container_tasks_state")
  |> filter(fn: (r) => r["name"] == name)
  |> filter(fn: (r) => r["guid"] == agent)
  |> filter(fn: (r) => r["state"] == "running")
  |> last()
  |> map(fn: (r) => ({_field:"cell_status",_value: "Ready"  ,_time:r._time,container:cell}))
`
)
